﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>确认支付</title>
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
    <link href="../css/ydui.css?v=1.1.4" rel="stylesheet" />
    <script src="../js/ydui.flexible.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/all.css?v=20180302" />
</head>
<body ms-controller="ordersure" class="ms-controller">
    <div class="g-flexview">
        <header class="m-navbar">
            <a onclick="history.back()" class="navbar-item"><i class="back-ico"></i></a>
            <div class="navbar-center"><span class="navbar-title">确认支付</span></div>
        </header>
        <section class="g-scrollview">
            <div class="m-cell">
                <div class="cell-item margin">
                    <div class="cell-left">订单编号：</div>
                    <div class="cell-right">{{@ModelData.ID}}</div>
                </div>
                <div class="cell-item margin">
                    <div class="cell-left">支付金额：</div>
                    <div class="cell-right" style="color:#ff0000;">
                        ¥{{@ModelData.SumPrice}}
                    </div>
                </div>
                <div class="cell-item margin">
                    <div class="cell-left">支付方式：</div>
                    <div class="cell-right">
                        微信支付
                    </div>
                </div>
            </div>
            <a class="btn  btn-primary" style="width:95%;margin:0.2rem;" ms-on-tap="@GoToPay()">
                立即支付
            </a>
        </section>
       

    </div>
    <script src="//cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="../js/ydui.js"></script>
    <script src="../js/avalon.mobile.min.js"></script>
    <script src="../js/ComFunJS.js?v=20180331"></script>
    <script>
        var model = avalon.define({
            $id: "ordersure",
            ModelData:{},
            ID: ComFunJS.getQueryString("id"),
            code: ComFunJS.getQueryString("code"),
            json:{},
            GetModelData: function () {
                if (model.ID) {
                    YDUI.dialog.loading.open("玩命加载中...");
                    $.getJSON("/api/order/modelh/"+model.ID, function (r) {
                        model.ModelData = r.result;

                        if (model.code) {
                            $.getJSON("/api1/weixin/payjsapi/" + model.ID + "/" + model.code, function (r0) {
                                YDUI.dialog.loading.close();
                                if (r0.error == "") {
                                    
                                    model.json = r0.result;
                                    if (typeof WeixinJSBridge == "undefined") {
                                        if (document.addEventListener) {
                                            document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                                        } else if (document.attachEvent) {
                                            document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                                            document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                                        }
                                    } else {
                                        jsApiCall();
                                    }

                                } else {
                                    YDUI.dialog.alert(r0.error);
                                }
                            })
                        } else {
                            YDUI.dialog.loading.close();
                        }

                    })
                }
            },
            GoToPay: function () {
                
                if (ComFunJS.isWeiXin()) {
                    var redirect_uri = encodeURI("http://www.ldx178.com/mobile/order/pay.html?id=" + model.ID + "&showwxpaytitle=1");
                    var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx56652205b133df19&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
                    window.location = url;
                } else {
                    $.getJSON("/api1/weixin/h5api/" + model.ID, function (r) {
                        if (r.error == "") {
                            var r_url = "http://www.ldx178.com/mobile/order/successpay.html?id=" + model.ID;

                            var url = r.result + "&redirect_url=" + encodeURI(r_url);
                            window.location = url;
                        } else {
                            YDUI.dialog.alert("支付失败，请重新支付");
                        }
                    });
                }

            }
        })
        avalon.ready(function () {

            //if (ComFunJS.isLogin(true)) {
                model.GetModelData()
            //}

        })

        //调用微信JS api 支付
        function jsApiCall() {
            WeixinJSBridge.invoke('getBrandWCPayRequest', model.json, function (res) {

                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    var r_url = "successpay.html?id=" + model.ID;

                    window.location = r_url;
                } else {
                    YDUI.dialog.alert("支付失败，请重新支付");
                }
                // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
            });
        }

    </script>
</body>
</html>
